package com.iwomy.secureplat.platform.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iwomy.secureplat.common.core.exception.PlatformBizErrorCodes;
import com.iwomy.secureplat.common.core.util.MsgUtils;
import com.iwomy.secureplat.common.core.util.R;
import com.iwomy.secureplat.common.log.annotation.SysLog;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.AlarmProcessTypeDTO;
import com.iwomy.secureplat.platform.entity.AlarmProcessTypeEntity;
import com.iwomy.secureplat.platform.exception.PlatformException;
import com.iwomy.secureplat.platform.service.AlarmProcessTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.iwomy.secureplat.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报警处理类型
 *
 * @author pig
 * @date 2023-12-18 18:50:44
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/alarmProcessType")
@Tag(description = "alarmProcessType", name = "报警处理类型管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AlarmProcessTypeController {

	private final AlarmProcessTypeService alarmProcessTypeService;

	/**
	 * 分页查询
	 *
	 * @param page             分页对象
	 * @param alarmProcessType 报警处理类型
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('platform_alarmProcessType_view')")
	public R getAlarmProcessTypePage(@ParameterObject Page page, @ParameterObject AlarmProcessTypeEntity alarmProcessType) {
		return R.ok(alarmProcessTypeService.pageByScope(page, alarmProcessType));
	}

	/**
	 * 列表
	 *
	 * @param alarmProcessType 报警处理类型
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/list")
	public R list(@ParameterObject AlarmProcessTypeEntity alarmProcessType) {
		return R.ok(alarmProcessTypeService.listByScope(alarmProcessType));
	}


	/**
	 * 通过id查询报警处理类型
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('platform_alarmProcessType_view')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(alarmProcessTypeService.getById(id));
	}

	/**
	 * 新增报警处理类型
	 *
	 * @param alarmProcessType 报警处理类型
	 * @return R
	 */
	@Operation(summary = "新增报警处理类型", description = "新增报警处理类型")
	@SysLog("新增报警处理类型")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmProcessType_add')")
	public R save(@RequestBody AlarmProcessTypeEntity alarmProcessType) {
		alarmProcessType.setProcessTypeCode(alarmProcessType.getProcessTypeCode());
		AlarmProcessTypeEntity wrapperEntity = new AlarmProcessTypeEntity();
		wrapperEntity.setProcessTypeName(alarmProcessType.getProcessTypeName());
		LambdaQueryWrapper<AlarmProcessTypeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		long count = alarmProcessTypeService.count(wrapper);
		if (count>0) {
//			R.failed("处理类型名称已存在");
			R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.ALARM_PROCESSTYPE_NAME_EXIST));
		}
		wrapperEntity = new AlarmProcessTypeEntity();
		wrapperEntity.setProcessTypeCode(alarmProcessType.getProcessTypeCode());
		wrapper = Wrappers.lambdaQuery(wrapperEntity);
		count = alarmProcessTypeService.count(wrapper);
		if (count>0) {
//			R.failed("处理类型ID已存在");
			R.failed(MsgUtils.getMessage(PlatformBizErrorCodes.ALARM_PROCESSTYPE_ID_EXIST));
		}
		alarmProcessType.setDeptId(SecurityUtils.getUser().getDeptId());
		return R.ok(alarmProcessTypeService.save(alarmProcessType));
	}

	/**
	 * 修改报警处理类型
	 *
	 * @param alarmProcessType 报警处理类型
	 * @return R
	 */
	@Operation(summary = "修改报警处理类型", description = "修改报警处理类型")
	@SysLog("修改报警处理类型")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmProcessType_edit')")
	public R updateById(@RequestBody AlarmProcessTypeEntity alarmProcessType) {


		AlarmProcessTypeEntity wrapperEntity = new AlarmProcessTypeEntity();
		wrapperEntity.setProcessTypeName(alarmProcessType.getProcessTypeName());
		LambdaQueryWrapper<AlarmProcessTypeEntity> wrapper = Wrappers.lambdaQuery(wrapperEntity);
		AlarmProcessTypeEntity one = alarmProcessTypeService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one) && !one.getId().equals(alarmProcessType.getId())) {
//			throw new PlatformException("处理类型名称已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.ALARM_PROCESSTYPE_NAME_USED_MODIFY));
		}
		wrapperEntity = new AlarmProcessTypeEntity();
		wrapperEntity.setProcessTypeCode(alarmProcessType.getProcessTypeCode());
		wrapper = Wrappers.lambdaQuery(wrapperEntity);
		one = alarmProcessTypeService.getOne(wrapper);
		if (ObjectUtil.isNotNull(one) && !one.getId().equals(alarmProcessType.getId())) {
//			throw new PlatformException("处理类型ID已存在");
			throw new PlatformException(MsgUtils.getMessage(PlatformBizErrorCodes.ALARM_PROCESSTYPE_ID_USED_MODIFY));
		}
		return R.ok(alarmProcessTypeService.updateById(alarmProcessType));
	}

	/**
	 * 通过id删除报警处理类型
	 *
	 * @param ids id列表
	 * @return R
	 */
	@Operation(summary = "通过id删除报警处理类型", description = "通过id删除报警处理类型")
	@SysLog("通过id删除报警处理类型")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('platform_alarmProcessType_del')")
	public R removeById(@RequestBody Long[] ids) {
		return R.ok(alarmProcessTypeService.removeBatchByIds(CollUtil.toList(ids)));
	}


	/**
	 * 导出excel 表格
	 *
	 * @param alarmProcessType 查询条件
	 * @param ids              导出指定ID
	 * @return excel 文件流
	 */
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('platform_alarmProcessType_export')")
	public List<AlarmProcessTypeEntity> export(AlarmProcessTypeEntity alarmProcessType, Long[] ids) {
		return alarmProcessTypeService.list(Wrappers.lambdaQuery(alarmProcessType).in(ArrayUtil.isNotEmpty(ids), AlarmProcessTypeEntity::getId, ids));
	}

	/**
	 * 启用禁用
	 *
	 * @param alarmProcessTypeDTO 处理类型
	 * @return R
	 */
	@Operation(summary = "启用禁用处理类型", description = "启用禁用处理类型")
	@SysLog(expression = "'启用禁用处理类型'.concat(#alarmProcessTypeDTO.remarks)")
	@PostMapping("/enableOrDisable")
	public R enableOrDisable(@RequestBody AlarmProcessTypeDTO alarmProcessTypeDTO) {
		AlarmProcessTypeEntity alarmProcessTypeEntity = new AlarmProcessTypeEntity();
		alarmProcessTypeEntity.setId(alarmProcessTypeDTO.getId());
		alarmProcessTypeEntity.setState(alarmProcessTypeDTO.getState());
		return R.ok(alarmProcessTypeService.updateById(alarmProcessTypeEntity));
	}

}